package com.test.exoplayertest.player.manager;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.OrientationEventListener;

import java.lang.ref.WeakReference;

/**
 * @author wuxiaotao on 2017/6/23.
 */

public class OrientationManager {

    private WeakReference<Activity> mActivity;
    private OrientationEventListener mOrientationEventListener;
    private boolean mPlayerIsLand = false; //默认竖屏
    private final String TAG = "orientation";
    private boolean mIsByUser = false; //手动切换横竖屏
    private boolean mUserLand = false; //手动切换横竖屏时，记录手动切换结果
    private final int DEGREE = 10; //灵敏度

    public OrientationManager(Activity activity) {
        mActivity = new WeakReference<Activity>(activity);
        startOrientationChangeListenner();
    }

    public boolean isPlayerIsLand() {
        return mPlayerIsLand;
    }

    private void startOrientationChangeListenner() {
        if (mActivity == null || mActivity.get() == null) {
            return;
        }
        mOrientationEventListener = new OrientationEventListener(mActivity.get()) {
            @Override
            public void onOrientationChanged(int orientation) {
                if (orientation > 0) {
                    if ((orientation > 90 - DEGREE && orientation < 90 + DEGREE) ||
                            (orientation > 270 - DEGREE && orientation < 270 + DEGREE)) {
                        if (mIsByUser && !mUserLand) { //手动切换到竖屏时，此时重力感应为横屏  丢弃操作
                            return;
                        }
                        setOrientationLand(false);
                    } else if (orientation < DEGREE || orientation > 360 - DEGREE) {
                        if (mIsByUser && mUserLand) { //手动切换到横屏时，此时重力感应为竖屏  丢弃操作
                            return;
                        }
                        setOrientationPort(false);
                    }
                }
            }
        };
        setSensorEnable(true);
    }

    /**
     * 设置横屏
     */
    public void setOrientationLand(boolean isByUser) {
        mIsByUser = isByUser;
        if (!mPlayerIsLand && mActivity != null && mActivity.get() != null) {
            mActivity.get().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            mPlayerIsLand = true;
            if (isByUser) {
                mUserLand = true;
            }
        }
    }

    /**
     * 设置竖屏
     */
    public void setOrientationPort(boolean isByUser) {
        mIsByUser = isByUser;
        if (mPlayerIsLand && mActivity != null && mActivity.get() != null) {
            mActivity.get().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            mPlayerIsLand = false;
            if (isByUser) {
                mUserLand = false;
            }
        }
    }

    public boolean setOrientationBtnClick() {
        if (mPlayerIsLand) {
            setOrientationPort(true);
        } else {
            setOrientationLand(true);
        }
        return mPlayerIsLand;
    }

    public void setSensorEnable(boolean isEnable) {
        if (mOrientationEventListener != null) {
            if (isEnable) {
                mOrientationEventListener.enable();
            } else {
                mOrientationEventListener.disable();
            }
        }
    }

    /**
     * 锁定当前屏幕方向
     */
    public void lockCurOrientation() {
        if (mActivity != null && mActivity.get() != null) {
            mActivity.get().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
        }
    }

    /**
     * 解除屏幕方向锁定
     */
    public void releaseOrientationLock() {
        if (mActivity != null && mActivity.get() != null) {
            mActivity.get().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
        }
    }
}
